package com.seek.eduservice.controller;


import com.seek.commonutils.ResponseResult;
import com.seek.eduservice.entity.EduChapter;
import com.seek.eduservice.entity.subject.Chapter;
import com.seek.eduservice.service.EduChapterService;
import com.seek.eduservice.service.EduVideoService;
import com.seek.servicebase.exceptionhandler.SeekException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wml
 * @since 2022-01-16
 */
@RestController
@RequestMapping("/eduservice/edu-chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private EduVideoService eduVideoService;

    // 课程大纲列表，根据课程 id 进行查询
    @GetMapping("getChapterAndVideo/{courseId}")
    public ResponseResult getChapterAndVideo(@PathVariable String courseId) {
        List<Chapter> list = eduChapterService.getChapterAndVideoByCourseId(courseId);
        return ResponseResult.ok().data("chapterAndVideoList", list);
    }

    // 添加章节
    @PostMapping("add")
    public ResponseResult add(@RequestBody EduChapter eduChapter) {
        eduChapterService.save(eduChapter);
        return ResponseResult.ok();
    }

    // 根据章节 id 查询
    @GetMapping("getById/{chapterId}")
    public ResponseResult getById(@PathVariable String chapterId) {
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return ResponseResult.ok().data("chapter", eduChapter);
    }

    // 根据章节 id 修改
    @PutMapping("updateById")
    public ResponseResult updateById(@RequestBody EduChapter eduChapter) {
        eduChapterService.updateById(eduChapter);
        return ResponseResult.ok();
    }

    // 根据章节 id 删除
    @DeleteMapping("deleteById/{chapterId}")
    public ResponseResult deleteById(@PathVariable String chapterId) {
        int count = eduVideoService.getByChapterId(chapterId);
        if (count == 0) {
            boolean flag = eduChapterService.removeById(chapterId);
            if (flag) {
                return ResponseResult.ok();
            } else {
                return ResponseResult.error();
            }
        } else {
            throw new SeekException(20001, "请先删除小节");
        }
    }

}

